package com.qianf.qfoffice.controller;

import com.github.pagehelper.Page;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.entity.Staff;
import com.qianf.qfoffice.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StaffController {


    @Autowired
    private StaffService staffService;


    @RequestMapping("/staffAll.do")
    @ResponseBody
    public Map<String,Object> findAllStaff (Integer page,Integer limit) {

        HashMap<String, Object> hashMap = new HashMap<>();

        List<Staff> allStaff = staffService.findAllStaff(page, limit);

        long total = ((Page)allStaff).getTotal();

        hashMap.put("code",0);
        hashMap.put("msg","");
        hashMap.put("count",total);
        hashMap.put("data",allStaff);

        return hashMap;

    }


    @RequestMapping("/staff/staffAdd.do")
    @ResponseBody
    public ResponseData addStaff (Staff staff) {


//        Staff staff = new Staff(uno, did, name, sex, email, qq, phone, entrytime);
        //String uno, String name, String did, String sex, String email,
        //                                  String phone, String qq, Date entrytime
        ResponseData<Object> responseData = new ResponseData<>();

        int i = staffService.addStaff(staff);

        if (i > 0) {
            responseData.setCode(1);
            responseData.setMsg("添加成功");
            return responseData;

        } else {

            responseData.setMsg("添加失败");
            return responseData;

        }
    }


    @RequestMapping("/staffdelete.do")
    @ResponseBody
    public ResponseData deleteStaff (int id) {

        ResponseData<Object> responseData = new ResponseData<>();

        int i = staffService.deleteStaff(id);

        if (i > 0) {
            responseData.setCode(1);
            responseData.setMsg("添加成功");
            return responseData;

        } else {

            responseData.setMsg("添加失败");
            return responseData;

        }

    }
}
